//
//  lyricHelper.m
//  LO_MUSIC
//
//  Created by lanou3g on 16/2/27.
//  Copyright © 2016年 徐强. All rights reserved.
//

#import "lyricHelper.h"
static lyricHelper *lyric = nil;
@implementation lyricHelper

//初始化
+ (instancetype)shareLyricHelper
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        lyric = [[lyricHelper alloc]init];
    });
    return lyric;
}
//初始化歌词
- (void)lyricWithStr:(NSString *)string
{
    if (self.modelArr.count) {
        //换歌时要将之前存放歌词的数组清空
        [self.modelArr removeAllObjects];
    }
    
    //根据换行符分离歌词
    if (string.length < 1 ) {
        return;
    }
    NSArray *lyricArr = [string componentsSeparatedByString:@"\n"];
    for (NSString *str in lyricArr) {
//        NSLog(@"%@",str);
        if (!str.length) {
            //如果单句歌词为空
            continue;
        }
        //根据"]"分离歌词
        NSArray *arr = [str componentsSeparatedByString:@"]"];
        NSString *time = nil;
        if([(NSString *)arr.firstObject length]){
            //时间与歌词
            time = [(NSString *)arr.firstObject substringFromIndex:1];
        }
        NSString *singleLyric = arr.lastObject;
        //alloc一个新的对象，讲本身作为一个model
        lyricHelper *helper = [[lyricHelper alloc]init];
        helper.lyric = singleLyric;
        helper.time = [self timeFromWithStr:time];
        
        //将模型添加到数组
        [self.modelArr addObject:helper];
        
    }
    
    
    
    
}
//根据时间获取下标
- (NSInteger)indexForTime:(float)time
{
    int i = 0 ;
    for (lyricHelper *ly in self.modelArr) {
        if (time < ly.time) {
//            return [self.modelArr indexOfObject:ly];
            break;
        }
        i++;
    }
    return i - 1;
}

- (float)timeFromWithStr:(NSString *)str{
    NSArray *arr = [str componentsSeparatedByString:@":"];
    float time = [arr.firstObject integerValue] * 60.0 + [arr.lastObject floatValue];
    return time;
}

- (NSMutableArray *)modelArr{
    if (!_modelArr) {
        _modelArr = [NSMutableArray array];
    }
    return _modelArr;
}
@end
